package com.personal.cuncunxing.module.home.sort;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.personal.cuncunxing.R;
import com.personal.cuncunxing.module.home.BaseFragment;

/**
 * Description:
 * Created by kode on 2020/5/26.
 */
public class SortFragment extends BaseFragment {
    private static final String TAG = "SortFragment";
    private RecyclerView recycler_view;
    private SortRVAdapter sortRVAdapter;

    @Override
    protected int setLayout() {
        return R.layout.home_sort_fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler_view = view.findViewById(R.id.sort_rv);
        //设置流布局
        recycler_view.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
        //设置适配器
        sortRVAdapter = new SortRVAdapter(getContext());
        recycler_view.setAdapter(sortRVAdapter);
        isViewCreated = true;
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d(TAG, "setUserVisibleHint: ->" + isVisibleToUser);
        if (isVisibleToUser) {
            isUIVisible = true;
            lazyLoad();
        } else {
            isUIVisible = false;
        }
    }

    //懒加载
    @Override
    protected void lazyLoad() {
        // //这里进行双重标记判断,是因为setUserVisibleHint会多次回调,并且会在onCreateView执行前回调,
        // 必须确保onCreateView加载完毕且页面可见,才加载数据
        if (isViewCreated && isUIVisible) {
            loadData();
            //数据加载完毕,恢复标记,防止重复加载
            isViewCreated = false;
            isUIVisible = false;
            Log.d(TAG, "lazyLoad:->加载数据");
        }
    }

    /**
     * 加载数据
     */
    private void loadData() {
    }
}
